<template> <!-- 导航栏 -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <div class="container">
    <router-link class="navbar-brand" :to="{name: 'HomeView'}">MySpace</router-link> <!-- router中的name -->
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarText">
      <ul class="navbar-nav me-auto mb-2 mb-lg-0">
        <li class="nav-item">
          <router-link class="nav-link active" aria-current="page" :to="{name : 'HomeView'}">首页</router-link>
        </li>
        <li class="nav-item">
          <router-link class="nav-link" :to="{name: 'UserListView'}">好友列表</router-link>
        </li>
        
      </ul>

      <ul class="navbar-nav" v-if="!$store.state.user.is_login"> <!-- 访问store的state中的user全局变量 -->
        <li class="nav-item">
          <router-link class="nav-link active" aria-current="page" :to="{name: 'LoginView'}">登录</router-link>
        </li>
        <li class="nav-item">
          <router-link class="nav-link" :to="{name: 'RegisterView'}">注册</router-link>
        </li> 
      </ul>

      <ul class="navbar-nav" v-else> <!-- 访问store的state中的user全局变量 -->
        <li class="nav-item">
          <router-link class="nav-link active" aria-current="page" :to="{name :'UserProfileView', params:{userId: $store.state.user.id}}">
            {{$store.state.user.username}}
          </router-link>
        </li>
        <li class="nav-item">
          <a class="nav-link" style="cursor: pointer" @click="logout">退出</a>
        </li>
      </ul>
    
    </div>
  </div>
</nav>
</template>


<script>
import { useStore } from 'vuex';
export default{
    name : "NavBar",

    setup() {
      const score = useStore();
      const logout = () => {
        score.commit("logout"); // 调用mutations:用commit, 调用actions用dispatch
      };
      return {
      logout,
    }
    },
}
</script>

<style scoped>
</style>